package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AssetManagementMapper;
import com.ruoyi.system.domain.AssetManagement;
import com.ruoyi.system.service.IAssetManagementService;

/**
 * 资产管理Service业务层处理
 *
 * @author 王帝
 * @date 2022-08-03
 */
@Service
public class AssetManagementServiceImpl implements IAssetManagementService {
    @Autowired
    private AssetManagementMapper assetManagementMapper;

    @Override
    public String importData(List<AssetManagement> assetManagementList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(assetManagementList) || assetManagementList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<AssetManagement> existList = selectAssetManagementList(null);
        for (AssetManagement importData : assetManagementList) {
            try {

                boolean userFlag = false;
                for (AssetManagement entry : existList) {
                    if (entry.getWorkNumber().equals(importData.getWorkNumber())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertAssetManagement(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getWorkNumber() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateAssetManagement(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getWorkNumber() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getWorkNumber() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + importData.getWorkNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询资产管理
     *
     * @param assetId 资产管理主键
     * @return 资产管理
     */
    @Override

    public AssetManagement selectAssetManagementByAssetId(Integer assetId) {
        return assetManagementMapper.selectAssetManagementByAssetId(assetId);
    }

    /**
     * 查询资产管理列表
     *
     * @param assetManagement 资产管理
     * @return 资产管理
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public List<AssetManagement> selectAssetManagementList(AssetManagement assetManagement) {
        return assetManagementMapper.selectAssetManagementList(assetManagement);
    }

    /**
     * 新增资产管理
     *
     * @param assetManagement 资产管理
     * @return 结果
     */
    @Override
    public int insertAssetManagement(AssetManagement assetManagement) {
        System.out.println("assetManagement = " + assetManagement);
        return assetManagementMapper.insertAssetManagement(assetManagement);
    }

    /**
     * 修改资产管理
     *
     * @param assetManagement 资产管理
     * @return 结果
     */
    @Override
    public int updateAssetManagement(AssetManagement assetManagement) {
        return assetManagementMapper.updateAssetManagement(assetManagement);
    }

    /**
     * 批量删除资产管理
     *
     * @param assetIds 需要删除的资产管理主键
     * @return 结果
     */
    @Override
    public int deleteAssetManagementByAssetIds(Integer[] assetIds) {
        return assetManagementMapper.deleteAssetManagementByAssetIds(assetIds);
    }

    /**
     * 删除资产管理信息
     *
     * @param assetId 资产管理主键
     * @return 结果
     */
    @Override
    public int deleteAssetManagementByAssetId(Integer assetId) {
        return assetManagementMapper.deleteAssetManagementByAssetId(assetId);
    }
}
